﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Persistent;
using S4Store.Domain;
using LX.Core.Query;
using S4Store.Domain.TelSaleKpi;
using S4Store.Service.Valid.TelSaleKpiValid;

namespace S4Store.Service
{
    public class TelSaleKpiService : BaseService
    {
        int comfirmstatus = DataConfirmStatus.Confirmed.ToInt();

        public CarBrandTelSaleMonthConfigSave CarBrandTelSaleMonthConfigSave { get; set; }

        public IQueryable<CarBrandTelSaleMonthConfig> GetCarBrandTelSaleMonthConfigQuery()
        {
            return this.GetQuery<CarBrandTelSaleMonthConfig>();
        }
        public IQueryable<CarBrandTelSaleDayData> GetCarBrandTelSaleDayDataQuery()
        {
            return this.GetQuery<CarBrandTelSaleDayData>();
        }
        public IQueryable<CarBrand> GetCarBrandQuery()
        {
            return this.GetQuery<CarBrand>();
        }

        public IQueryable<CarBrand> GetCarbrandsForStoreMonth(int year, int month,int storeid)
        {
            var query = this.GetQuery<CarBrandTelSaleMonthConfig>();
            var result = from q in query
                         where q.IsConfirmed == true &&
                               q.StoreId == storeid &&
                               q.Month.Year == year &&
                               q.Month.Month == month
                         select q.CarBrand;
            return result.Distinct();
        }

        public IQueryable<CarBrand> GetCarbrandsForStoreMonths(DateTime start, DateTime end, int storeid)
        {
            DateTime s = new DateTime(start.Year, start.Month, 1);
            DateTime e = new DateTime(end.Year, end.Month, 1).MonthLastOrNow();
            e = new DateTime(e.Year, e.Month, e.Day, 23, 59, 59);
            var query = this.GetQuery<CarBrandTelSaleMonthConfig>();
            var result = from q in query
                         where q.IsConfirmed == true &&
                               q.StoreId == storeid &&
                               q.Month>=s &&
                               q.Month<=e
                         select q.CarBrand;
            return result.Distinct();
        }




        public CarBrandTelSaleMonthConfig GetCarKpiMonthConfig(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                CarBrandTelSaleMonthConfig config = this.GetCarBrandTelSaleMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
                if (config == null)
                {
                    config = new CarBrandTelSaleMonthConfig();
                    config.Month = new DateTime(year, month, 1);
                    config.StoreId = storeid;
                }
                return config;
            }
            else
            {
                CarBrandTelSaleMonthConfig config = this.GetCarBrandTelSaleMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
                if (config == null )
                {
                    config = new CarBrandTelSaleMonthConfig();
                    config.Month = new DateTime(year, month, 1);
                    config.StoreId = storeid;
                }
                return config;
            }
        }



        public void SubmitCarBrandTelSaleMonthConfig(int year, int month, int storeid, List<CarBrandTelSaleMonthConfig> configs)
        {
            var CarBrandTelSaleMonthConfigs = GetCarBrandTelSaleMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            foreach (var Carc in CarBrandTelSaleMonthConfigs)
            {
                this.GetSet<CarBrandTelSaleMonthConfig>().Remove(Carc);
            }
            foreach (var config in configs)
            {
                this.GetSet<CarBrandTelSaleMonthConfig>().Add(config);
            }
        }

        public IQueryable<CarBrandTelSaleMonthConfig> GetCarBrandTelSaleMonthConfig(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return GetCarBrandTelSaleMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
            }
            else
            {
                return GetCarBrandTelSaleMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            }
        }

        public IQueryable<CarBrandTelSaleDayData> GetCarBrandTelSaleDayDatas(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return GetCarBrandTelSaleDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.StoreId == storeid && t.IsConfirmed);
            }
            else
            {
                return GetCarBrandTelSaleDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.StoreId == storeid);
            }
        }


        public IQueryable<CarBrandTelSaleDayData> GetTelSaleKpiDayDatas(int year, int month, int day, int storeid)
        {
            return GetCarBrandTelSaleDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.Date.Day == day && t.StoreId == storeid);
        }

        public void SubmitTelSaleKpiDayDatas(int year, int month, int day, int storeid, List<CarBrandTelSaleDayData> days)
        {

            var carbarndsinplan = this.GetCarbrandsForStoreMonth(year, month, storeid).ToList();

            if (days.Any(t => carbarndsinplan.All(x => x.Id != t.CarBrandId)))
            {
                throw new Exception("提交的数据里包含未做月初计划的车型");
            }



            var datas = GetCarBrandTelSaleDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.Date.Day == day && t.StoreId == storeid);
            foreach (var data in datas)
            {
                this.GetSet<CarBrandTelSaleDayData>().Remove(data);
            }
            foreach (var config in days)
            {
                this.GetSet<CarBrandTelSaleDayData>().Add(config);
            }
        }


        public TelSaleKpiStandard SaveTelSaleKpiStandard(TelSaleKpiStandard model)
        {
            var standard = this.GetQuery<TelSaleKpiStandard>().FirstOrDefault(t => t.Id == model.Id);
            if (standard != null)
            {
                standard.H12DHHFLV = model.H12DHHFLV;
                standard.KHDJLV = model.KHDJLV;
                standard.XSKHZHLV = model.XSKHZHLV;
                standard.LDKHXXLULV = model.LDKHXXLULV;
            }
            return standard;
        }

        public TelSaleKpiStandard GetTelSaleKpiStandard(int year, int month, int storeid)
        {
            var model = this.GetQuery<TelSaleKpiStandard>().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            if (model == null)
            {
                model = new TelSaleKpiStandard();
                model.Month = new DateTime(year, month, 1);
                model.StoreId = storeid;
                var date = new DateTime(year, month, 1).AddMonths(-1);
                var lyear = date.Year;
                var lmonth = date.Month;
                var lastmodel = this.GetQuery<TelSaleKpiStandard>().FirstOrDefault(t => t.Month.Year == lyear && t.Month.Month == lmonth && t.StoreId == storeid);
                if (lastmodel != null)
                {
                    model.H12DHHFLV = lastmodel.H12DHHFLV;
                    model.KHDJLV = lastmodel.KHDJLV;
                    model.XSKHZHLV = lastmodel.XSKHZHLV;
                    model.LDKHXXLULV = lastmodel.LDKHXXLULV;
                }
                this.GetSet<TelSaleKpiStandard>().Add(model);
            }
            return model;
        }


    }
}
